package com.csjbot.weedle_auto.Slam.agent.Util

import android.os.Environment

import com.csjbot.weedle_auto.Slam.agent.Entity.MapBean
import com.csjbot.weedle_auto.Slam.agent.Entity.MapLines
import com.csjbot.weedle_auto.Slam.agent.Entity.MapWalls
import com.csjbot.weedle_auto.Slam.events.MapOperationEvent
import com.slamtec.slamware.AbstractSlamwarePlatform
import com.slamtec.slamware.geometry.Line
import com.slamtec.slamware.robot.ArtifactUsage

import org.greenrobot.eventbus.EventBus

import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.util.ArrayList

/**
 * Created by chenqi on 2017/2/27.
 */

object FileUtil {

    /**
     * 判断文件是否存在
     * @param path
     */
    fun judeFileExists(path: String) {
        val file = File(path)
        if (file.exists()) {
            println("file exists")
        } else {
            println("file not exists, create it ...")
            try {
                file.createNewFile()
            } catch (e: IOException) {
                // TODO Auto-generated catch block
                e.printStackTrace()
            }

        }

    }

    /**
     * 获取内置SD卡路径
     * @return
     */
    val innerSDCardPath: String
        get() = Environment.getExternalStorageDirectory().path


    /**
     * 判断文件夹是否存在
     * @param path
     */
    fun judeDirExists(path: String): Boolean {
        val file = File(path)
        if (file.exists()) {
            if (file.isDirectory) {
                println("dir exists")
            } else {
                println("the same name file exists, can not create dir")
            }
        } else {
            println("dir not exists, create it ...")
            file.mkdir()
        }
        return file.exists()
    }

    /**
     * 存在本地的地图
     *
     * @param map
     */
    fun saveMapToFile(map: MapBean) {
        var fos: FileOutputStream? = null
        var oos: ObjectOutputStream? = null
        val f = File(Constants.path)
        try {
            fos = FileOutputStream(f)
            oos = ObjectOutputStream(fos)
            oos.writeObject(map)    //括号内参数为要保存java对象
        } catch (e: FileNotFoundException) {
            e.printStackTrace()
        } catch (e: IOException) {
            e.printStackTrace()
        } finally {
            try {
                oos!!.close()
                fos!!.close()
                EventBus.getDefault().post(MapOperationEvent(MapOperationEvent.SAVEMAPOK))
            } catch (e: IOException) {
                e.printStackTrace()
            }

        }
    }

    /**
     * 从本地文件读取
     *
     * @return
     */
    fun read(): Any? {
        var ois: ObjectInputStream? = null
        var people: Any? = null
        try {
            //获取输入流
            ois = ObjectInputStream(FileInputStream(File(Constants.path)))
            //获取文件中的数据
            people = ois.readObject()
            return people
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            try {
                if (ois != null) {
                    ois.close()
                }
            } catch (e: IOException) {
                e.printStackTrace()
            }

        }
        return people
    }

    /**
     * get track
     *
     * @param map
     * @return
     */
    fun getTrack(map: MapBean): List<Line> {
        val tracks = ArrayList<Line>()
        for (i in 0..map.lines!!.size - 1) {
            val track = Line(map.lines!![i].segmentId,
                    map.lines!![i].line_startPoint_x,
                    map.lines!![i].line_startPoint_y,
                    map.lines!![i].line_endPoint_x,
                    map.lines!![i].line_endPoint_y)
            tracks.add(track)
        }
        return tracks
    }

    /**
     * get walls
     *
     * @param map
     * @return
     */
    fun getWall(map: MapBean): List<Line> {
        val walls = ArrayList<Line>()
        for (i in 0..map.walls!!.size - 1) {
            val track = Line(map.walls!![i].segmentId,
                    map.walls!![i].line_startPoint_x,
                    map.walls!![i].line_startPoint_y,
                    map.walls!![i].line_endPoint_x,
                    map.walls!![i].line_endPoint_y)
            walls.add(track)
        }
        return walls
    }

    /**
     * save wall
     *
     * @param robotPlatform
     * @return
     */
    fun SaveWalls(robotPlatform: AbstractSlamwarePlatform): List<MapWalls> {

        val walls = robotPlatform.getLines(ArtifactUsage.ArtifactUsageVirutalWall)
        val mapWalls = ArrayList<MapWalls>()
        //实例化当前需要存储的数据
        for (w in walls.indices) {
            val mapWall = MapWalls(walls[w].segmentId, walls[w].startX, walls[w].startY, walls[w].endX, walls[w].endY)
            mapWalls.add(mapWall)
        }
        return mapWalls
    }

    /**
     * save track
     *
     * @param robotPlatform
     * @return
     */
    fun SaveTrack(robotPlatform: AbstractSlamwarePlatform): List<MapLines> {
        val line = robotPlatform.getLines(ArtifactUsage.ArtifactUsageVirtualTrack)
        val maplines = ArrayList<MapLines>()
        for (i in line.indices) {
            val mapline = MapLines(line[i].segmentId,
                    line[i].startX,
                    line[i].startY,
                    line[i].endX,
                    line[i].endY)
            maplines.add(mapline)
        }
        return maplines
    }
}
